#!/usr/bin/perl
#save_serv.cgi

require "./mdmsearch-lib.pl";
&ReadParse();

$s=&get_server($in{'id'});
#&header($text{'edit_title'}, "");

if ($in{'deal'} eq "del"){
	&del_serv($in{'id'});
	&header($text{'delete_title'}, "");
	print "<table class=mode1 border=0 cellpadding=4 cellspacing=1 width=100%>\n";
	print "<tr><td>$text{'meg_delsuccess1'}\n";
	print "<a href='../mdmgroups/index.cgi'>\n";
	print "$text{'mdmgroup'}</a>\n";
	print "$text{'meg_delsuccess2'}</td></tr>\n";
	print "</table>\n";
	&footer("index.cgi",$text{'index_title'});
	
}else{
	if (($in{'hostname'} =~ /[^A-Za-z0-9\x7f-\xff\_\-\(\)]/)||($in{'hostname'} eq "")){
		$errormsg = $errormsg.<p>.$text{'err_hostname_invalid'};	
		&error($errormsg);
	}
	if (length($in{'hostname'}) > 14){
		$errormsg = $errormsg.<p>.$text{'err_hostname_toolong'};
	}

	if ($in{'ip'} eq ""){
		$errormsg = $errormsg.<p>.$text{'err_noip'};
	}elsif(($in{'ip'}=~ /^\D/)){
		$errormsg = $errormsg.<p>.$text{'err_wrongip'};
	}elsif(!&check_ipaddress($in{'ip'})){
		$errormsg = $errormsg.<p>.$text{'err_wrongip'};
	}

	if ($in{'controlled'} eq 'yes'){
		if ($in{'user'}){
			&foreign_require('setpass','setpass-lib.pl');
			local $result=&foreign_call('setpass','check_simbol_uname',$in{'user'});
			if ($result){
				$errormsg = $errormsg.<p>.$result;
			}	
		}else{
			$errormsg = $errormsg.<p>.$text{'err_nouser'};
		}

		if (length($in{'passwd'})>8){
			$errormsg = $errormsg.<p>.$text{err_passwd_toolong};
		}
		
		if(!$errormsg){
			$s->{'user'}=$in{'user'};
			$s->{'passwd'}=$in{'passwd'};
		}
	}else{
		$s->{'user'}='';
		$s->{'passwd'}='';
	}

	if($errormsg){
		&error($errormsg);
	}else{
		&save_old_serv($s->{'id'},$in{'hostname'},$in{'ip'},$s->{'user'},$s->{'passwd'});
	}
	
	if ($in{'controlled'} eq 'yes'){
		&remote_error_setup(\&inst_error);
		local $result = &sure_user($in{'id'});
		if ($result =~ /\s/){
			&save_old_serv($s->{'id'},$in{'hostname'},$in{'ip'},'','');
			local @date;
			push @data,$result;
			&foreign_call("mdmhistory","mdm_log","To control a device",\@data);	
			&error($result);
		}
	}
	&redirect("index.cgi");
}

1;